package comet.kernel;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import comet.web.SDGView;
import comet.web.searchUsingSwoogle;
import comet.web.searchUsingWatson;

@SuppressWarnings("serial")
public class SDGController extends HttpServlet {

    ServletContext contexte;
    String terms;
	String ontologyType;
	String searchEngine; 

	public void doGet(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException
	{
		traiter(request,response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException
	{
		traiter(request,response);
	}

	public void traiter(HttpServletRequest request, HttpServletResponse response)
			throws IOException
	{
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
				
		String terms = request.getParameter("terms");
		String ontologyType = request.getParameter("ontologyType");
		String searchEngine = request.getParameter("searchEngine");
		out.println("Terms: " + terms);
		out.println("ontologyType: " +ontologyType);
		out.println("searchEngine: " +searchEngine);
		
		SDGView sdgview=null;
		int size=0;
		
		if(searchEngine.equalsIgnoreCase("watson")){
			sdgview = new searchUsingWatson(terms,ontologyType);
			sdgview.searchWebForOntology();
			
		} 
		
		if(searchEngine.equalsIgnoreCase("swoogle")){
			sdgview = new searchUsingSwoogle(terms,ontologyType);
			sdgview.searchWebForOntology();
						
		} 
		
		out.println("Table size: " +  sdgview.getListOfUri().size()); 
		
		ArrayList<String> list = new ArrayList<String>();
		list = sdgview.getListOfUri();
		for (int i=0; i<list.size(); i++) 	out.println(list.get(i));
		
	}
}

